package com.ctgu.chatapp

import com.ctgu.chatapp.Netty.protocol.Message
import java.util.concurrent.ConcurrentHashMap

object TaskTimerManager {
    private val mMsgTimeOutMap = ConcurrentHashMap<Long, TaskTimer>()

    fun add(imsMsg: Message.IMSMsg) {
        if (imsMsg.head != null) {
            mMsgTimeOutMap[imsMsg.head.timestamp] = TaskTimer(imsMsg)
        }
    }

    fun remove(id: Long): Message.IMSMsg? {
        val timer = mMsgTimeOutMap.remove(id)
        return if (timer != null) {
            timer.cancel()
            val imsMsg = timer.task.imsMsg
            println("从消息队列中移除发给${imsMsg.head.toId}的消息")
            timer.task.imsMsg
        } else null
    }
}